//
//  NSString+PLExtension.m
//  PL.local
//
//  Created by 刘军军 on 17/5/25.
//
//

#import "NSString+PLExtension.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (PLExtension)

- (NSString *)pl_stringByRemoveingWhitespace {
    // 过滤“通讯录”拷贝的特殊字符
    NSString *str = [self stringByReplacingOccurrencesOfString:@"\\p{Cf}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
    // 去除空格 ！！！以下两个空格为不同类型！！！
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    // 去除其他空白字符
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return str;
}

- (NSString *)pl_formatChinaMobileNumber {
    // 去掉电话中的特殊字符
    NSString *str = [self stringByReplacingOccurrencesOfString:@"+86" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"-" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"(" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@")" withString:@""];
    // 去掉空格
    str = [str pl_stringByRemoveingWhitespace];
    return str;
}

/** 转成中文大写金额 */
- (NSString *)pl_chineseYuan{
    //先把self转成NSDecimalNumber类型, 并保留2位小数(第3位开始舍弃)
    NSDecimalNumber *decimalValue = [NSDecimalNumber decimalNumberWithString:self];
    NSDecimalNumberHandler *handle = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
    decimalValue = [decimalValue decimalNumberByRoundingAccordingToBehavior:handle];
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    // NSLocale的意义是将货币信息、标点符号、书写顺序等进行包装，如果app仅用于中国区应用，为了保证当用户修改语言环境时app显示语言一致，则需要设置NSLocal（不常用）
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];// 全拼格式
    formatter.numberStyle = NSNumberFormatterSpellOutStyle;
    // 小数点后最少位数
    formatter.minimumFractionDigits = 2;
    // 小数点后最多位数
    formatter.maximumFractionDigits = 6;
    formatter.formatterBehavior = NSNumberFormatterBehaviorDefault;
    //得到一串字符 eg:一千二百三十四
    NSString *numString = [formatter stringFromNumber:decimalValue];
    //手动替换中文小写为大写
    numString = [numString stringByReplacingOccurrencesOfString:@"〇" withString:@"零"];
    numString = [numString stringByReplacingOccurrencesOfString:@"一" withString:@"壹"];
    numString = [numString stringByReplacingOccurrencesOfString:@"二" withString:@"贰"];
    numString = [numString stringByReplacingOccurrencesOfString:@"三" withString:@"叁"];
    numString = [numString stringByReplacingOccurrencesOfString:@"四" withString:@"肆"];
    numString = [numString stringByReplacingOccurrencesOfString:@"五" withString:@"伍"];
    numString = [numString stringByReplacingOccurrencesOfString:@"六" withString:@"陆"];
    numString = [numString stringByReplacingOccurrencesOfString:@"七" withString:@"柒"];
    numString = [numString stringByReplacingOccurrencesOfString:@"八" withString:@"捌"];
    numString = [numString stringByReplacingOccurrencesOfString:@"九" withString:@"玖"];
    numString = [numString stringByReplacingOccurrencesOfString:@"十" withString:@"拾"];
    numString = [numString stringByReplacingOccurrencesOfString:@"百" withString:@"佰"];
    numString = [numString stringByReplacingOccurrencesOfString:@"千" withString:@"仟"];
    numString = [numString stringByReplacingOccurrencesOfString:@"万" withString:@"萬"];
    //小数点处理为角和分
    if ([numString rangeOfString:@"点"].length>0){
        //有小数点
        NSArray *numberArray = [numString componentsSeparatedByString:@"点"];
        NSString *integer = numberArray.firstObject;
        NSString *decimal = numberArray.lastObject;
        numString = [integer stringByAppendingString:@"圆"];
        NSString *jiao = [decimal substringToIndex:1];
        if (![jiao isEqualToString:@"零"]) {
            jiao = [jiao stringByAppendingString:@"角"];
        }
        numString = [numString stringByAppendingString:jiao];
        if (decimal.length>=2) {
            NSString *fen = [[decimal substringFromIndex:1] stringByAppendingString:@"分"];
            numString = [numString stringByAppendingString:fen];
        }
    }else{
        //无小数点
        numString = [numString stringByAppendingString:@"圆整"];
    }
    return numString;
}

-(NSDate *)pl_toDateWithFormat:(NSString *)format{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = format;
    return [dateFormatter dateFromString:self];
}

-(CGSize)pl_sizeWithFont:(UIFont *)font maxWidth:(CGFloat)maxWidth{
    return [self pl_sizeWithFont:font maxSize:CGSizeMake(maxWidth, 0)];
}

-(CGSize)pl_sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize{
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:font} context:nil].size;
}

@end
